package cn.xxstudy.compose.material

import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import cn.xxstudy.compose.ui.theme.ComposeDemoTheme
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch

/**
 * @author ：wuxinxi on 2021/3/8 .
 * @packages ：cn.xxstudy.compose.material .
 * TODO:底部sheet、侧滑
 */
class SimpleBottomModalBottomSheetLayoutActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            ComposeDemoTheme {
                val intent = intent
                val modalType = intent?.getIntExtra("modalType", 0) ?: 0
                if (modalType == 0) {
                    BottomModalBottomSheetLayoutMain()
                } else {
                    ModalDrawerMain()
                }
            }
        }
    }
}

@Preview
@Composable
fun BottomModalBottomSheetLayoutMain() {
    val scope = rememberCoroutineScope()
    val activity = LocalContext.current as SimpleBottomModalBottomSheetLayoutActivity
    Scaffold(
        topBar = {
            TopAppBar(title = { Text(text = "BottomModalBottomSheetLayout") }, navigationIcon = {
                IconButton(onClick = { scope.launch { activity.finish() } }) {
                    Icon(Icons.Default.ArrowBack, "")
                }
            })
        },
        content = { BottomModalBottomSheetLayoutContent() }
    )
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun BottomModalBottomSheetLayoutContent() {
    val state = rememberModalBottomSheetState(initialValue = ModalBottomSheetValue.Hidden)
    val scope = rememberCoroutineScope()
    ModalBottomSheetLayout(
        sheetState = state,
        sheetContent = {
            LazyColumn(content = {
                items(50) {
                    ListItem(
                        icon = { Icon(Icons.Default.Favorite, "") },
                        text = { Text(text = "Sheet Item -> $it") }
                    )
                }
            })
        },
        content = {
            LazyColumn(content = {
                items(50) {
                    ListItem(
                        icon = {
                            Icon(
                                if (it == 0) Icons.Default.List else Icons.Default.Favorite,
                                ""
                            )
                        },
                        text = { Text(text = if (it == 0) "展开Sheet" else "Content UI Item -> $it") },
                        modifier = Modifier.clickable {
                            if (it == 0) {
                                scope.launch { state.show() }
                            }
                        }
                    )
                }
            })
        }
    )
}

@Composable
fun ModalDrawerMain() {
    val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
    val scope = rememberCoroutineScope()
    val activity = LocalContext.current as SimpleBottomModalBottomSheetLayoutActivity
    Scaffold(
        topBar = {
            TopAppBar(title = { Text("Drawer") }, navigationIcon = {
                IconButton(onClick = {
                    scope.launch {
                        if (drawerState.isClosed) {
                            drawerState.open()
                        } else {
                            drawerState.close()
                        }
                    }
                }) {
                    Icon(
                        if (drawerState.isClosed) Icons.Default.Menu else Icons.Default.Close,
                        ""
                    )
                }
            },
                actions = {
                    IconButton(onClick = { scope.launch { activity.finish() } }) {
                        Icon(Icons.Default.Close, "")
                    }
                })
        },
        content = {
            ModalDrawerComponent(scope, drawerState)
        }
    )
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun ModalDrawerComponent(scope: CoroutineScope, drawerState: DrawerState) {

    ModalDrawer(
        drawerState = drawerState,
        drawerContent = {
            LazyColumn(content = {
                items(10) {
                    ListItem(text = { Text("Drawer Item -> $it") }, modifier = Modifier.clickable {
                        if (it == 0) {
                            scope.launch { drawerState.close() }
                        }
                    })
                }
            })
        }, content = {
            LazyColumn(content = {
                items(50) {
                    ListItem(text = { Text("Content Item -> $it") })
                }
            })
        })

}